package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcServer;
import com.csun.cmny.provider.model.dto.server.ServerDto;
import com.csun.cmny.provider.model.dto.server.ServerQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcServerService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-09-02 14:05
 * @Description: the class dmc server controller
 */
@RestController
@RequestMapping(value = "/server", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcServerController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcServerService dmcServerService;

    /**
     * add dmc server
     *
     * @param serverDto the server dto
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper add(@Validated @RequestBody ServerDto serverDto, BindingResult br) {

        DmcServer dmcServer = new DmcServer();
        BeanUtils.copyProperties(serverDto, dmcServer);
        Integer result = dmcServerService.add(dmcServer, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * edit dmc server
     *
     * @param serverDto the server dto
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@RequestBody ServerDto serverDto) {

        DmcServer dmcServer = new DmcServer();
        BeanUtils.copyProperties(serverDto, dmcServer);
        Integer result = dmcServerService.edit(dmcServer, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * delete dmc server by id
     *
     * @param id    the dmc server id
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = dmcServerService.deleteByKey(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * query dmc server list with page
     *
     * @param serverQueryDto    the server query dto
     *
     * @return      the wrapper
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody ServerQueryDto serverQueryDto) {

        PageHelper.startPage(serverQueryDto.getPageNum(), serverQueryDto.getPageSize());

        List<DmcServer> dmcServerList = dmcServerService.queryListWithPage(serverQueryDto);

       // return WrapMapper.ok(new PageInfo<>(dmcServerList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),new PageInfo<>(dmcServerList));
    }

}
